package com.woniuxy.project.campus.canteen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.project.campus.canteen.mapper.ProductGroupMapper;
import com.woniuxy.project.campus.canteen.mapper.ShopsMapper;
import com.woniuxy.project.campus.canteen.model.ProductType;
import com.woniuxy.project.campus.canteen.mapper.ProductTypeMapper;
import com.woniuxy.project.campus.canteen.model.Shops;
import com.woniuxy.project.campus.canteen.param.AddProductGroupParam;
import com.woniuxy.project.campus.canteen.param.DeleteProductTypeParam;
import com.woniuxy.project.campus.canteen.param.UpdateProductTypeParam;
import com.woniuxy.project.campus.canteen.service.ProductTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.project.campus.canteenModelform.AddProductTypeForm;
import com.woniuxy.project.campus.util.TokenTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 猫将军
 * @since 2025年02月17日
 */
@Service
public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements ProductTypeService {

    @Resource
    private ProductTypeMapper productTypeMapper;
    @Resource
    private ShopsMapper shopsMapper;
    @Override
    public void addProductGroup(AddProductGroupParam bean) {
        Integer id = TokenTool.getUserIdByToken(bean.getToken());
        QueryWrapper<Shops> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",id);
        Shops shops = shopsMapper.selectOne(queryWrapper);
        if(shops==null){
            throw new RuntimeException("该用户没有商铺");
        }
        ProductType productType = new ProductType();
        //查询所有的商品类型
        QueryWrapper<ProductType> productTypeQueryWrapper = new QueryWrapper<>();
        productTypeQueryWrapper.eq("shop_id",shops.getId());
        List<ProductType> productTypes = productTypeMapper.selectList(productTypeQueryWrapper);
        productTypeQueryWrapper.clear();
        //根据序号查询商品类型
        QueryWrapper<ProductType> productTypeQueryWrapper1 = new QueryWrapper<>();
        productTypeQueryWrapper1.eq("shop_id",shops.getId()).eq("type_number",bean.getSort());
        ProductType productType1 = productTypeMapper.selectOne(productTypeQueryWrapper1);
        if(productType1!=null){
            throw new RuntimeException("该序号已存在");
        }

        for(ProductType productType2:productTypes){
            if(productType2.getTypeNumber()>=bean.getSort()) {
                productType2.setTypeNumber(productType2.getTypeNumber() + 1);
                productTypeMapper.updateById(productType2);
            }
            productType.setTypeNumber(bean.getSort());
        }
        //查询所有类型
        if(productTypes.isEmpty()&&bean.getSort()==1){
            productType.setTypeNumber(bean.getSort());
        }else if(productTypes.isEmpty()&&bean.getSort()!=1) {
            throw new RuntimeException("这是第一个商品类型，排序只能为1");
        }else if(bean.getSort()>=productTypes.size()+1) {
            productType.setTypeNumber(bean.getSort());
        }

        productType.setProductTypeName(bean.getProductTypeName());
        productType.setShopId(shops.getId());
        productTypeMapper.insert(productType);
    }

    @Override
    public List<ProductType> queryAllProductType(String token) {
        Integer id = TokenTool.getUserIdByToken(token);
        QueryWrapper<Shops> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",id);
        Shops shops = shopsMapper.selectOne(queryWrapper);
        if(shops==null){
            throw new RuntimeException("该用户没有商铺");
        }
        QueryWrapper<ProductType> productTypeQueryWrapper = new QueryWrapper<>();
        productTypeQueryWrapper.eq("shop_id",shops.getId());
        List<ProductType> productTypes = productTypeMapper.selectList(productTypeQueryWrapper);
        return productTypes;
    }

    @Override
    public void deleteProductType(DeleteProductTypeParam bean) {
        Integer id = TokenTool.getUserIdByToken(bean.getToken());
        QueryWrapper<Shops> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",id);
        Shops shops = shopsMapper.selectOne(queryWrapper);
        if(shops==null){
            throw new RuntimeException("该用户没有商铺");
        }

        QueryWrapper<ProductType> productTypeQueryWrapper = new QueryWrapper<>();
        productTypeQueryWrapper.eq("shop_id",shops.getId()).eq("id",bean.getProductTypeId());
        productTypeMapper.delete(productTypeQueryWrapper);
    }

    @Override
    public void updateProductType(UpdateProductTypeParam bean) {
        Integer id = TokenTool.getUserIdByToken(bean.getToken());
        QueryWrapper<Shops> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",id);
        Shops shops = shopsMapper.selectOne(queryWrapper);
        if(shops==null){
            throw new RuntimeException("该用户没有商铺");
        }
        QueryWrapper<ProductType> productTypeQueryWrapper = new QueryWrapper<>();
        productTypeQueryWrapper.eq("shop_id",shops.getId()).eq("id",bean.getTypeId());
        ProductType productType = productTypeMapper.selectOne(productTypeQueryWrapper);
        if(productType==null){
            throw new RuntimeException("该商品类型不存在");
        }
        productType.setProductTypeName(bean.getTypeName());
        productTypeMapper.updateById(productType);
    }

    @Override
    public void addProductType(AddProductTypeForm form, String token) {
        Integer id = TokenTool.getUserIdByToken(token);
        QueryWrapper<Shops> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",id);
        Shops shops = shopsMapper.selectOne(queryWrapper);
        if(shops==null){
            throw new RuntimeException("该用户没有商铺");
        }
        //查询类型
        QueryWrapper<ProductType> productTypeQueryWrapper = new QueryWrapper<>();
        productTypeQueryWrapper.eq("shop_id",shops.getId());

        //查询所有类型是否有重复
        List<ProductType> productTypes = productTypeMapper.selectList(productTypeQueryWrapper);
        for(ProductType productType1:productTypes){
            if(productType1.getProductTypeName().equals(form.getProductTypeName())){
                throw new RuntimeException("该类型已存在");
            }

        }
        ProductType productType = new ProductType();
        productType.setProductTypeName(form.getProductTypeName());
        productType.setShopId(shops.getId());
        productTypeMapper.insert(productType);
    }


}
